/* File makes Table 13 panel B  */

/* 	Input files: 	declarations, stock_day, crsp_returns
	Output files: T13b_ret_stockday.tex */

**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************


use declarations, clear 

gcollapse (sum) bull bear (count) userid, by(symbolid date)

ren userid n_declared
gen pct_bull=bull/n_declared

drop bull bear
compress
save declarations_symbolday, replace  


**************************************************************
**************************************************************
 
use stock_day, clear 
merge 1:1 date permno using crsp_returns, keep(match master) nogen 
	
	foreach k in cabret_l1to5 cabret_l6to30 xret {  
	replace `k' = `k'*100
	} 
	forval k=2/31 {
	replace xret_f`k'=xret_f`k'*100
	}
drop ablogvol_f* 

merge 1:1 date symbolid using declarations_symbolday, keep(master match) nogen 

save stock_day_ret, replace  


gen asvi5_abret5=asvi_l5day * cabret_l1to5 

scalar controls ="i.media asvi sdabret_l1to5 cabret_l1to5 cabret_l6to30 asvi_l5day asvi5_abret5"  
scalar FE ="i.permno#i.ym i.date i.mbin*"  

reghdfe ablogvol std_orig_signals std_rec_mean_signal ablogvol_l1 `=controls', absorb(`=FE') vce(cl permno date)  

keep if e(sample)==1 

qui sum pct_bull,d  
gen hi_bull=cond(pct_bull>=r(p50),1,0) // 91%
drop symbolid nsignals_l5day n_declared ablogvol_l1 comnam  xret_f3 - xret_f31  pct_bull

save temp_stock_day_ret_toUse, replace


use temp_stock_day_ret_toUse, clear

scalar clvars = "permno date"
scalar FE ="i.permno#i.ym i.date i.mbin*"  
scalar controls ="i.media asvi sdabret_l1to5 cabret_l1to5 cabret_l6to30 xret " 

eststo ret1_5d: reghdfe xret_5day_f2 i.hi_bull `=controls' , absorb(`=FE') vce(cl `=clvars') 
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)
eststo ret7_5d: reghdfe xret_5day_f2 i.hi_bull##c.std_orig_signals std_rec_mean_signal i.hi_bull#c.std_rec_mean_signal mean_rec_signal_std i.hi_bull#c.mean_rec_signal_std  `=controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)

eststo ret8_10d: reghdfe xret_10day_f2 i.hi_bull##c.std_orig_signals std_rec_mean_signal i.hi_bull#c.std_rec_mean_signal mean_rec_signal_std i.hi_bull#c.mean_rec_signal_std  `=controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)
eststo ret9_30d: reghdfe xret_30day_f2 i.hi_bull##c.std_orig_signals std_rec_mean_signal i.hi_bull#c.std_rec_mean_signal mean_rec_signal_std i.hi_bull#c.mean_rec_signal_std  `=controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)

********************************************


cap n estadd local dayfe5 "Y" : * 
cap n estadd local monthyearfe5 "Y" : *
cap n estadd local mbinfe5 "Y" : *

local droplist "_cons 0.media 0.hi_bull 0.hi_bull#c.std_orig_signals 0.hi_bull#c.std_rec_mean_signal 0.hi_bull#c.mean_rec_signal_std"
local orderlist "1.hi_bull std_orig_signals 1.hi_bull#c.std_orig_signals std_rec_mean_signal 1.hi_bull#c.std_rec_mean_signal mean_rec_signal_std 1.hi_bull#c.mean_rec_signal_std 1.media asvi sdabret_l1to5 xret"

local stats_opts " stats(N N_clust1 N_clust2 r2 dayfe5 monthyearfe5 mbinfe5, fmt(%18.0fc %18.0fc %18.0fc %12.2fc %12.2fc) labels("\# obs." "\# clusters (stock)" "\# clusters (day)" "$\textit{R}^2$" "Day FE" "Month x stock FE" "Message number FE") ) " 

#delimit ; 
local opts "  b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles (
"to t+5"
"to t+5"
"to t+10"
"to t+30" )
mgroups("Cumulative abnormal returns\textsubscript{s,(t+1 $\rightarrow$ ... )}" 
, pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;        

cd "`=tabledir'" ;
esttab ret* using T13b_ret_stockday.tex, `opts' `stats_opts'  drop(`droplist') order(`orderlist') coef(
1.hi_bull "$\mathbbm{1}$ if >median Bullishness\textsubscript{s,t}"
std_orig_signals "Sender disagreement\textsubscript{s,t}" 
1.hi_bull#c.std_orig_signals "$\mathbbm{1}$ if >median Bullishness\textsubscript{s,t} $\times$ Sender disagreement\textsubscript{s,t}"
std_rec_mean_signal "Received disagreement\textsubscript{s,t}" 
1.hi_bull#c.std_rec_mean_signal "$\mathbbm{1}$ if >median Bullishness\textsubscript{s,t} $\times$ Received disagreement\textsubscript{s,t}"
mean_rec_signal_std "Received uncertainty\textsubscript{s,t}" 
1.hi_bull#c.mean_rec_signal_std "$\mathbbm{1}$ if >median Bullishness\textsubscript{s,t} $\times$ Received uncertainty\textsubscript{s,t}"
1.media "$\mathbbm{1}$ Media article\textsubscript{s,t}"
asvi "log GoogleASVI\textsubscript{s,t}"
sdabret_l1to5 "Volatility\textsubscript{s,(t-5 to t-1)}"
xret "Abnormal returns\textsubscript{s,t}"  
cabret_l1to5 "Cum. abnormal returns\textsubscript{s,(t-5 to t-1)}"
cabret_l6to30 "Cum. abnormal returns\textsubscript{s,(t-30 to t-6)}"
ablogvol "Abnormal log turnover\textsubscript{s,t}" 
asvi_l5day "log GoogleASVI\textsubscript{s,(t-5 to t-1)}"
asvi5_abret5 "(log GoogleASVI $\times$ Cum. ab. ret.)\textsubscript{s,(t-5 to t-1)}" 
) replace  ;
#delimit cr 
cd "`=maindir'"


cap n erase temp_stock_day_ret_toUse.dta

